package cn.hncj.lyc.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: tz
 * @Date: 2019/3/17 21:22
 */
@Configuration
public class MyConfig implements WebMvcConfigurer {

    @Autowired
   private InterceptorConfig interceptorConfig;
    @Autowired
    private FilterConfig filterConfig;
    

    @Override
    //方法用来专门注册一个Handler，来处理静态资源的，例如：图片，js，css等
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }


    // 这个方法用来注册拦截器，我们自己写好的拦截器需要通过这里添加注册才能生效
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(interceptorConfig).addPathPatterns();
        registry.addInterceptor(interceptorConfig).addPathPatterns("/back/**").excludePathPatterns("/back/login").excludePathPatterns("/back/login_back");
        registry.addInterceptor(filterConfig).addPathPatterns("/admin/ordercheck/**").excludePathPatterns("/login").excludePathPatterns("/register");
    }

    @Override
    //方法可以很方便的实现一个请求到视图的映射，而无需书写controller
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index");

        //registry.addViewController("/admin").setViewName("forward:/admin/adminlogin");

        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
}
